/**
 * @author: Miss.杨
 * @date: 2025年3月08日 16:02:47
 * @description: 
 */
package com.sheepone.monitor.audit.core.entities;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson2.JSON;
import com.sheepone.monitor.proto.AuditLogMessageRequest;
import com.sheepone.util.time.TimeFormatter;
import com.sheepone.webdev.log.WebDevLogger;

/**
 * @author: Miss.杨
 * @date: 2025年3月08日 16:02:47
 * @description:
 */
public class AuditLogMessage {
    /**
     * 服务类型
     */
    private String service;
    /**
     * 应用id
     */
    private String appId;
    /**
     * 模块
     */
    private String module;
    /**
     * 行为
     */
    private String action;
    /**
     * 请求时刻
     */
    private LocalDateTime requestTime;
    /**
     * 请求耗时
     */
    private Double duration;
    /**
     * 请求状态
     */
    private String status;
    /**
     * 用户唯一标识
     */
    private String userId;

    private Integer resourceId;

    /**
     * Server端进行的业务复查核心内容（以json字符串形式传输）
     */
    private String data;

    private Map<String, Object> ext;

    private static Integer MAX_EXT_VALUE_SIZE = 16;
    private static WebDevLogger logger = WebDevLogger.getLogger(AuditLogMessage.class);

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public LocalDateTime getRequestTime() {
        return requestTime;
    }

    public void setRequestTime(LocalDateTime requestTime) {
        this.requestTime = requestTime;
    }

    public Double getDuration() {
        return duration;
    }

    public void setDuration(Double duration) {
        this.duration = duration;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = JSON.toJSONString(data);
    }

    public Integer getResourceId() {
        return resourceId;
    }

    public void setResourceId(Integer resourceId) {
        this.resourceId = resourceId;
    }

    public AuditLogMessageRequest toRequest() {
        AuditLogMessageRequest.Builder builder = AuditLogMessageRequest.newBuilder();
        builder.setService(this.service);
        builder.setResourceId(resourceId);
        builder.setAppId(this.appId);
        builder.setModule(this.module);
        builder.setAction(this.action);
        builder.setRequestTime(TimeFormatter.formatLocalDateTime(requestTime));
        builder.setDuration(this.duration);
        builder.setStatus(this.status);
        builder.setUserId(this.userId);
        builder.setData(this.data == null && this.ext != null ? JSON.toJSONString(this.ext)
                : (this.data == null ? "" : this.data));
        return builder.build();
    }

    public static AuditLogMessage fromRequest(AuditLogMessageRequest request) {
        AuditLogMessage message = new AuditLogMessage();
        message.setService(request.getService());
        message.setResourceId(request.getResourceId());
        message.setAppId(request.getAppId());
        message.setModule(request.getModule());
        message.setAction(request.getAction());
        message.setRequestTime(LocalDateTime.parse(request.getRequestTime()));
        message.setDuration(request.getDuration());
        message.setStatus(request.getStatus());
        message.setUserId(request.getUserId());
        message.setData(JSON.parse(request.getData()));
        return message;
    }

    public void putDataValue(String key, Object value) {
        if (data != null) {
            logger.warn("message's data is existed,cannot set additional values.");
            return;
        }
        if (ext == null) {
            ext = new HashMap<>(MAX_EXT_VALUE_SIZE);
        } else if (ext.size() >= MAX_EXT_VALUE_SIZE) {
            logger.warn("message's data keys are full, cannot accommodate additional values.");
            return;
        }
        ext.put(key, value);
    }

}
